package org.springblade.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.pm.entity.PmWeeknote;
import org.springblade.pm.entity.PmWeeknoteEvaluate;
import org.springblade.pm.mapper.PmWeeknoteEvaluateMapper;
import org.springblade.pm.mapper.PmWeeknoteMapper;
import org.springblade.pm.service.IPmWeeknoteService;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class PmWeeknoteServiceImpl extends BaseServiceImpl<PmWeeknoteMapper, PmWeeknote> implements IPmWeeknoteService {

	PmWeeknoteMapper pmWeeknoteMapper;
	PmWeeknoteEvaluateMapper pmWeeknoteEvaluateMapper;
	IUserClient userClient;

	@Override
	public Map<String, Object> selectWeekById(Map<String, Object> params) {
		return pmWeeknoteMapper.selectWeekById(params);
	}
	@Override
	public List<Map<String, Object>> selectWeekList(Map<String, Object> map) {
		User user = userClient.userInfo(Long.valueOf((Integer)map.get("userId"))).getData().getUser();
		String roleIdString = user.getRoleId();
		List<String> roleIds = Arrays.asList(roleIdString.split(","));
		if (roleIds.contains("1")||roleIds.contains("3")){
			map.put("flag","true");
			return pmWeeknoteMapper.selectWeekDeptId(map);
		}else if(roleIds.contains("8")){
			map.put("deptId",user.getDeptId());
			return pmWeeknoteMapper.selectWeekDeptId(map);
		}else  {
			return pmWeeknoteMapper.selectWeekList(map);
		}
	}

	@Override
	public boolean deleteWeekById(Integer id) {

		try {
			if(pmWeeknoteMapper.deleteById(id)>0){
				UpdateWrapper<PmWeeknoteEvaluate> wrapper = new UpdateWrapper<>();
				wrapper.eq("weeknote_id",id);
				pmWeeknoteEvaluateMapper.delete(wrapper);
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<Map<String, Object>> getManager(Map<String, Object> params) {
		return pmWeeknoteMapper.getManager(params);
	}

	@Override
	public String selectWeekByProjectId(Integer proId) {
		return pmWeeknoteMapper.selectWeekByProjectId(proId);
	}


	@Override
	public boolean submit(PmWeeknote weeknote) {
		return saveOrUpdate(weeknote);
	}
}
